﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PopShotImpactController : MonoBehaviour {


	private Animator anim;

	// singleton list to hold all our projectiles
	static private List<PopShotImpactController> popShotImpactControllers;


	//--------------------------------------------------------------------------
	// static public methods
	//--------------------------------------------------------------------------
	static public void Reset()
	{
		// for each projectile
		foreach(PopShotImpactController popShotImpactController in popShotImpactControllers)
		{
			// configure it
			popShotImpactController.Configure();
		}
	}

	static public PopShotImpactController Spawn(Vector3 location)
	{
		// search for the first free popShotImpactController
		foreach(PopShotImpactController popShotImpactController in popShotImpactControllers)
		{
			// if disabled, then it's available
			if(popShotImpactController.gameObject.activeSelf == false)
			{
				// set it up
				popShotImpactController.transform.position = location;
				// Set the angle - this is purely cosmetic.
				popShotImpactController.transform.Rotate(0,0, 45);
				popShotImpactController.gameObject.SetActive(true);

				// return a reference to the caller
				return popShotImpactController;
			}
		}
		// we normally wouldn't get here unless the player is firing rapidly
		// in some games we always want the pool request to succeed, if so
		// then we would instantiate another one here if we needed
		// in our case, it's part of the design that you can only have
		// a limited number of shots active at any one time
		return null;
	}

	//--------------------------------------------------------------------------
	// protected mono methods
	//--------------------------------------------------------------------------
	protected void Awake()
	{
		// does the pool exist yet?
		if(popShotImpactControllers == null)
		{
			// lazy initialize it
			popShotImpactControllers = new List<PopShotImpactController>();
		}
		// add myself
		popShotImpactControllers.Add(this);
	}

	protected void OnDestroy()
	{
		// remove myself from the pool
		popShotImpactControllers.Remove(this);
		// was I the last one?
		if(popShotImpactControllers.Count == 0)
		{
			// remove the pool itself
			popShotImpactControllers = null;
		}
	}

	protected void Start()
	{
		anim = GetComponent<Animator>();
		gameObject.SetActive(false);
	}

	protected void Update()
	{
		//Check to see if animation is done playing
		if(anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 1)
		{
			gameObject.SetActive(false);
		}
		
	}

//	bool AnimatorIsPlaying()
//	{
//		return animator.GetCurrentAnimatorStateInfo(0).length >
//			animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
//	}
	//--------------------------------------------------------------------------
	// private methods
	//--------------------------------------------------------------------------
	private void Configure()
	{
		// just disable myself so I'm available for use
		gameObject.SetActive(false);
	}
}

